using UnityEngine;

public class GlassEdge : MonoBehaviour
{
	private float glassMaxWidth;

	private Transform glassFront;

	private float[] glassMaskWidth;

	private Vector3 glassMaskPosition;

	private void Awake()
	{
		glassFront = base.transform.FindDeepChild("GlassFront");
		GenerateEdgePoints();
		CalculateGlassMaskWidth();
		glassMaskPosition = base.transform.FindDeepChild("LiquidMask").position;
	}

	public float GetGlassWidthAtHeight(float liquidHeight)
	{
		float num = liquidHeight - glassMaskPosition.y;
		int num2 = Mathf.RoundToInt(num * 100f);
		int num3 = Mathf.RoundToInt(glassMaskWidth.Length);
		int num4 = num3 + num2;
		if (num4 >= 0 && num4 < glassMaskWidth.Length)
		{
			return glassMaskWidth[num3 + num2];
		}
		return 0f;
	}

	private void CalculateGlassMaskWidth()
	{
		Sprite sprite = base.transform.FindDeepChild("LiquidMask").GetComponent<SpriteMask>().sprite;
		Texture2D texture = sprite.texture;
		glassMaskWidth = new float[texture.height];
		Color[] array = null;
		int num = 0;
		for (int i = 0; i < texture.height; i++)
		{
			array = texture.GetPixels(0, i, texture.width, 1);
			num = 0;
			for (int j = 0; j < array.Length; j++)
			{
				if (array[j].a > 0.5f)
				{
					num++;
				}
			}
			glassMaskWidth[i] = (float)num / 100f;
		}
	}

	private void GenerateEdgePoints()
	{
		Transform transform = base.transform.FindDeepChild("Edge");
		int childCount = transform.childCount;
		if (childCount == 2)
		{
			Vector3 position = transform.GetChild(0).position;
			float x = position.x;
			Vector3 position2 = transform.GetChild(1).position;
			float x2 = position2.x;
			glassMaxWidth = Mathf.Abs(x2 - x);
		}
	}

	public float GetGlassMaxWidth()
	{
		return glassMaxWidth;
	}

	public Vector3 GetGlassUpperEdgePosition()
	{
		return glassFront.GetComponent<SpriteRenderer>().bounds.max;
	}
}
